<?php

$auth = @$_SESSION['auth'];
if (empty($auth)) die('用户未登录！^_^');

if (!check_account_addr($auth['id'])) die('e1');

$topic_id = intval($_GET['topic_id']);

$topic = dt_query_one("SELECT order_c_s, start_t, start_t_s, order_l, order_l_n, out_s FROM forum_topic WHERE id = $topic_id");
if (!$topic) die('获取topic数据失败！^_^');

$cond = "WHERE user_id = ".$auth['id']." AND topic_id = $topic_id AND complete = 0";
$has_c_order = (dt_query_one("SELECT id FROM forum_topic_order ".preg_replace('/complete = 0/', 'complete = 1', $cond, 1)." LIMIT 1")) ? 1 : 0;   //是否有已完成的订单，仿刷订单

if (!dt_query_one("SELECT id FROM forum_topic_order $cond LIMIT 1")) {
	if (1 == $topic['start_t'] && time() < $topic['start_t_s']) die('违规的操作，未开始！^_^');    //是否开放下单
	if (2 == $topic['start_t'] && time() > $topic['start_t_s']) die('违规的操作，已结束！^_^');    //是否结束下单
	if (1 == $topic['order_l'] && $topic['order_c_s'] >= $topic['order_l_n']) die('订单已达上限！^_^');    //是否达到订单上限

	$rs = dt_query("INSERT INTO forum_topic_order (user_id, topic_id, c_at) VALUES (".$auth['id'].", $topic_id, ".time().")");
	if (!$rs) die('新增forum_topic_order数据失败！');
	$rs = dt_query("UPDATE forum_topic SET order_c = order_c + 1, order_c_s = CASE WHEN $has_c_order = 0 THEN order_c_s + 1 ELSE order_c_s END WHERE id = $topic_id");
	if (!$rs) die('统计forum_topic数据失败！');

	if (1 == $topic['out_s']) die('s2');
	die('s0');
} else {
	$rs = dt_query("DELETE FROM forum_topic_order $cond");
	if (!$rs) die('删除forum_topic_order数据失败！');
	$rs = dt_query("UPDATE forum_topic SET order_c = order_c - 1, order_c_s = CASE WHEN $has_c_order = 0 THEN order_c_s - 1 ELSE order_c_s END WHERE id = $topic_id");
	if (!$rs) die('统计forum_topic数据失败！');
	die('s1');
}
